#ifndef SPHERE_H
#define SPHERE_H

#include "hittable.h"
#include "../vec3.h"

class sphere : public hittable {
public:
	// 静止球体
	sphere(const point3& static_center, double radius, std::shared_ptr<material> mat)
		: center(static_center, vec3(0, 0, 0)), radius(std::fmax(0, radius)), mat(mat)
	{
		auto rvec = vec3(radius, radius, radius);
		bbox = aabb(static_center - rvec, static_center + rvec);
	}

	// 移动球体
	sphere(const point3& center1, const point3& center2, double radius, std::shared_ptr<material> mat)
		: center(center1, center2 - center1), radius(std::fmax(0, radius)), mat(mat)
	{
		auto rvec = vec3(radius, radius, radius);
		aabb box1(center.at(0) - rvec, center.at(0) + rvec);
		aabb box2(center.at(1) - rvec, center.at(1) + rvec);
		bbox = aabb(box1, box2);
	}

	aabb bounding_box() const override { return bbox; }

	bool hit(const ray& r, interval ray_t, hit_record& rec) const override;

	static void get_sphere_uv(const point3& p, double& u, double& v);

	double pdf_value(const point3& origin, const vec3& direction) const override;
	vec3 random(const point3& origin) const override;

private:
	static vec3 random_to_sphere(double radius, double distance_squared);

	ray center;
	double radius;
	std::shared_ptr<material> mat;
	aabb bbox;
};

#endif